﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace XFFSM
{
    [CustomEditor(typeof(FSMStateInspectorHelper))]
    public class FSMStateInspector : Editor
    {
        private string stateName;

        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();

            FSMStateInspectorHelper helper = target as FSMStateInspectorHelper;
            if (helper == null) return;

            bool disabled = EditorApplication.isPlaying
                || helper.node.name.Equals(FSMConst.anyState)
                || helper.node.name.Equals(FSMConst.entryState);

            EditorGUI.BeginDisabledGroup(disabled);

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("StateName:", GUILayout.Width(80));
            helper.node.StateName = EditorGUILayout.TextField(helper.node.StateName);

            if (helper.node.StateName != null && !helper.node.StateName.Equals(stateName))
            {
                stateName = helper.node.StateName;
                EditorUtility.SetDirty(helper.controller);
            }

            EditorGUILayout.EndHorizontal();


            EditorGUI.EndDisabledGroup();
        }

        protected override void OnHeaderGUI()
        {
            //base.OnHeaderGUI();
            FSMStateInspectorHelper helper = target as FSMStateInspectorHelper;
            if (helper == null) return;


            string name = null;
            EditorGUI.BeginChangeCheck();
            {
                EditorGUILayout.Space();
                EditorGUILayout.BeginHorizontal();

                GUILayout.Label(EditorGUIUtility.IconContent("icons/processed/unityeditor/animations/animatorstate icon.asset"), GUILayout.Width(30), GUILayout.Height(30));

                EditorGUILayout.LabelField("Name:", GUILayout.Width(60));

                bool disabled = EditorApplication.isPlaying
                    || helper.node.name.Equals(FSMConst.anyState)
                    || helper.node.name.Equals(FSMConst.entryState);

                EditorGUI.BeginDisabledGroup(disabled);
                name = EditorGUILayout.DelayedTextField(helper.node.name);
                EditorGUI.EndDisabledGroup();

                EditorGUILayout.EndHorizontal();
            }
            if (EditorGUI.EndChangeCheck())
            {
                //Debug.Log("重命名:TODO");
                FSMStateNodeFactory.Rename(helper.controller, helper.node, name);
            }
            EditorGUILayout.Space();
            var rect = EditorGUILayout.BeginHorizontal();

            Handles.color = Color.black;
            Handles.DrawLine(new Vector2(rect.x, rect.y), new Vector3(rect.x + rect.width, rect.y));

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
        }

    }


}

